#pragma once
#include <filetransfferstruct.h>
#include <string>
#include <memory>
#include <unordered_map>

class FileTransfferMgr {
public:
	static FileTransfferMgr& getInstance() {
		static FileTransfferMgr mgr;
		return mgr;
	}

	/// <summary>
	/// 添加filestruct。调用该函数，将filestruct添加到openfiles_中。
	/// </summary>
	/// <param name="pFileStruct"></param>
	/// <returns></returns>
	bool appendFileStruct(std::shared_ptr<FileTransfferStruct> pFileStruct);

	/// <summary>
	/// 判断会话是否访问文件
	/// </summary>
	/// <param name="sessionId">会话id</param>
	/// <param name="filename">文件名</param>
	/// <returns></returns>
	bool existFileStruct(const std::string& sessionId, const std::string& filename);

	/// <summary>
	/// 获取fileStruct对象。用于外部读写filestruct
	/// </summary>
	/// <param name="sessionId">会话id</param>
	/// <param name="filename">文件名</param>
	/// <returns></returns>
	std::shared_ptr<FileTransfferStruct> getFileStruct(const std::string& sessionId, const std::string& filename);

	/// <summary>
	/// 移除filestruct。在文件处理结束后调用移除openfiles_中的对应的filestruct
	/// </summary>
	/// <param name="sessionId">会话id</param>
	/// <param name="fileName">文件名</param>
	/// <returns></returns>
	bool removeFileStruct(const std::string& sessionId, const std::string& fileName);

	bool removeFileStruct(const std::string& sessionId);

	~FileTransfferMgr() = default;

private:
	FileTransfferMgr() = default;

private:
	std::unordered_map<std::string, std::unordered_map<std::string, std::shared_ptr<FileTransfferStruct>>> openfiles_; // 记录各个session打开的文件
};